package ConnectionsCommands;

import Gear.Factory;

import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implement received command to register a new file.
 *
 * @author Rodrigo Martins Racanicci
 */
public class ConnectionCommandNewFile implements ConnectionCommand {

    @Override
    public void execute(Socket client, String[] args) {
        try {
            Factory.getInstance().getTracker().getFileMutex().acquire();

            if (!Factory.getInstance().getTracker().getFileNames().contains(args[1])) {
                Factory.getInstance().getTracker().sendCommand(client, "true");
                Factory.getInstance().getTracker().addFile(client.getInetAddress().toString(), args[1]);
            } else {
                Factory.getInstance().getTracker().sendCommand(client, "false");
            }
            Factory.getInstance().getTracker().closeClient(client);

            Factory.getInstance().getTracker().getFileMutex().release();

        } catch (InterruptedException ex) {
            Logger.getLogger(ConnectionCommandNewFile.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

}
